<template>
  <div class="homepage">
    <a-layout-header class="header">
      <div class="header-content">
        <div class="left">
          <img src="/src/assets/image/sztu_icon.webp" alt="SZTU图标" class="sztu-icon" />
          <h1 class="title">离散数学教学辅助系统</h1>
        </div>
        <div class="right">
          <span class="user-info">教师端</span>
          <a-dropdown>
            <a-avatar :size="32" icon="user" class="avatar" style="cursor: pointer" />
            <template #overlay>
              <a-menu>
                <a-menu-item key="logout" @click="handleLogout">
                  <LogoutOutlined />
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
      </div>
    </a-layout-header>
    
    <div class="main-container">
      <!-- 左侧菜单栏 -->
      <div class="sidebar">
        <a-menu
          v-model:selectedKeys="selectedKeys"
          mode="inline"
          @click="handleMenuClick"
        >
          <a-menu-item key="class-management">
            <template #icon>
              <UsergroupAddOutlined />
            </template>
            <span>班级管理</span>
          </a-menu-item>
          <a-menu-item key="question-bank">
            <template #icon>
              <FileSearchOutlined />
            </template>
            <span>题库管理</span>
          </a-menu-item>
          <a-menu-item key="animation-demo">
            <template #icon>
              <PlayCircleOutlined />
            </template>
            <span>动画演示</span>
          </a-menu-item>
        </a-menu>
      </div>
      
      <!-- 右侧内容区域 -->
      <div class="content">
        <!-- 班级管理内容 -->
        <div v-if="currentView === 'class-management' && !showClassDetail">
          <ClassList />
        </div>
        
        <!-- 班级详情内容 -->
        <div v-else-if="showClassDetail">
          <ClassManagement :id="currentClassId" />
        </div>
        
        <!-- 题库管理内容 -->
        <div v-else-if="currentView === 'question-bank'">
          <QuestionBank />
        </div>
        
        <!-- 动画演示内容 -->
        <div v-if="currentView === 'animation-demo'">
          <Animation />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { message } from 'ant-design-vue';
import { UsergroupAddOutlined, PlayCircleOutlined, FileSearchOutlined, LogoutOutlined } from '@ant-design/icons-vue';
import Animation from './Animation.vue';
import ClassList from './ClassList.vue';
import ClassManagement from './ClassManagement.vue';
import QuestionBank from './QuestionBank.vue';
import { removeToken } from '../utils/token';

const selectedKeys = ref(['class-management']);
const currentView = ref('class-management');
const showClassDetail = ref(false);
const currentClassId = ref(null);
const router = useRouter();

const handleMenuClick = ({ key }) => {
  console.log('选中的菜单项:', key);
  currentView.value = key;
  // 如果不是班级管理页面，隐藏班级详情
  if (key !== 'class-management') {
    showClassDetail.value = false;
  }
};

// 暴露方法给子组件，用于显示班级详情
const showClassManagement = (classId) => {
  currentClassId.value = classId;
  showClassDetail.value = true;
};

// 退出登录
const handleLogout = () => {
  removeToken();
  message.success('已退出登录');
  router.push('/login');
};

// 将方法添加到全局属性，供子组件调用
defineExpose({
  showClassManagement
});
</script>

<style scoped>
.header {
  background: #fff;
  padding: 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  height: 100px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 24px;
  height: 100%;
}

.left {
  display: flex;
  align-items: center;
}

.sztu-icon {
  width: 80px;
  height: 80px;
  margin-right: 16px;
}

.title {
  color: #003366;
  font-size: 2rem;
  font-weight: 700;
  margin: 0;
  font-family: 'Segoe Script', 'Brush Script MT', cursive;
}

.user-info {
  margin-right: 12px;
  color: rgba(0, 0, 0, 0.65);
}

.avatar {
  background-color: #1890ff;
}

.main-container {
  display: flex;
  margin-top: 100px;
  height: calc(100vh - 100px);
  position: relative; /* 添加相对定位 */
}

.sidebar {
  width: 250px;
  background-color: #ffffff;
  border-right: 1px solid #e0e0e0;
  padding: 20px 0;
  flex-shrink: 0;
  position: fixed; /* 固定定位 */
  top: 100px; /* 距离顶部的高度 */
  left: 0;
  bottom: 0;
  overflow-y: auto; /* 如果菜单项过多，菜单本身可以滚动 */
}

.content {
  flex: 1;
  padding: 20px;
  background-color: #fafafa;
  overflow-y: auto;
  margin-left: 250px; /* 为固定定位的侧边栏留出空间 */
}
</style>